//
//  RecommendGameView.swift
//  DouYu
//
//  Created by 王鹏宇 on 2018/4/13.
//  Copyright © 2018年 王鹏宇. All rights reserved.
//

import UIKit

private let KGameCellID = "KGameCellID"
private let KEdgeInsetMargin : CGFloat = 10

class RecommendGameView: UIView {

    //MARK:- 控件属性
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    //MARK:-定义数据属性
    var groups : [AnchorGroup]? {
        
        didSet {
            //赋值后刷新表格
            collectionView.reloadData()
        }
    }
    
    override func awakeFromNib() {
        
        super.awakeFromNib()
        
        // 让控件不随这父控件的拉伸儿拉伸
        autoresizingMask = UIViewAutoresizing()
        
        collectionView.dataSource = self;
        
        
        // 注册Cell
        collectionView.register(UINib(nibName:"CollectionGameCell", bundle: nil), forCellWithReuseIdentifier: KGameCellID)
        
        //给collectionView添加内边距
        
        collectionView.contentInset = UIEdgeInsets(top: 0, left: KEdgeInsetMargin, bottom: 0, right: KEdgeInsetMargin)
    }
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}

// MARK: - 提供快速创建的类的方法

extension RecommendGameView {
    
    class func recommendGameView()-> RecommendGameView {
        
        return Bundle.main.loadNibNamed("RecommendGameView", owner: nil, options: nil)?.first as! RecommendGameView
    }
}


// MARK:- 遵守UICollectionView的数据源协议
extension RecommendGameView : UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return groups?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KGameCellID, for: indexPath) as! CollectionGameCell
        
        cell.baseGame = groups![indexPath.item]
        
        return cell;
    }
}
